package org.easy.tool.controller;

import org.easy.tool.common.R;
import org.easy.tool.entity.Constant;
import org.easy.tool.entity.Stock;
import org.easy.tool.service.IStock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashMap;
import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Valley
 * @since 2024-02-27 21:58:59
 */
@RestController
@RequestMapping("/stock")
public class StockController {

    @Autowired
    IStock stockService;


    /**
     * 功能：获取排序好的列表
     */
    @RequestMapping("getStockList")
    public R getStockList() {
        try {
            long start = System.currentTimeMillis();
            List<LinkedHashMap> stockList = stockService.getStockList(Constant.codeList);
            long time = System.currentTimeMillis() - start;
            return R.ok(stockList, time);
        } catch (Throwable e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

    /**
     * 功能：获取所有数据中最优数据列表
     */
    @RequestMapping("getAllGoodStock")
    public R getAllGoodStock(@RequestBody List<String> codeList) {
        try {
            long start = System.currentTimeMillis();
            List<LinkedHashMap> stockList = stockService.getStockList();
            long time = System.currentTimeMillis() - start;
            return R.ok(stockList, time);
        } catch (Throwable e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

    /**
     * 功能：获取实时股票信息
     */
    @RequestMapping("getNowStock")
    public R getNowStock(@RequestBody LinkedHashMap param) {
        try {
            String code = (String) param.get("code");
            Stock stock = stockService.getStockNowData(code);
            return R.ok(stock);
        } catch (Throwable e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

    /**
     * 功能：新增股票数据
     */
    @RequestMapping("refreshAllDate")
    public R refreshAllDate(@RequestBody LinkedHashMap param) {
        try {
            String start = (String) param.get("start");
            String end = (String) param.get("end");
            int stockSize = stockService.refreshDateData(start, end);
            return R.ok(stockSize);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

    /**
     * 功能：新增股票数据
     */
    @RequestMapping("searchStockRate")
    public R searchStockRate(@RequestBody LinkedHashMap param) {
        try {
            String code = (String) param.get("code");
            LinkedHashMap map = stockService.getHistoryDayStock(code);
            return R.ok(map);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

}
